package cn.chainco.cts.security.validator;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.StrutsStatics;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import cn.chainco.cts.action.ActionUtil;
import cn.chainco.cts.iservice.CommonManager;
import cn.chainco.cts.jdo.Employee;
import cn.chainco.cts.jdo.Work;
import cn.chainco.cts.jdo.WorkDoc;
import cn.chainco.cts.security.BinsinessInterceptor;
import cn.chainco.cts.utils.CommUtils;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;

public class BusinessValidator extends HibernateDaoSupport{
	private CommonManager commonManager;
	
	public int addOrEditWork(ActionInvocation actionInvocation){
		int result = BinsinessInterceptor.ACCESS_DENIAL;
		try{
			Employee employee = ActionUtil.getSelectEmployee();
			if(employee != null && employee.getCompany() != null){
				ActionContext actionContext = actionInvocation.getInvocationContext();
				HttpServletRequest request = (HttpServletRequest)actionContext.get(StrutsStatics.HTTP_REQUEST);
				String workId = request.getParameter("workId");
				if(!CommUtils.isCorrectInt(workId)){
					result = BinsinessInterceptor.ACCESS_PERMIT;
				}else{
					Work work = this.commonManager.findEntityByPK(Work.class, Long.parseLong(workId));
					if(work != null && work.getCompany() != null && work.getCompany().getId() == employee.getCompany().getId()){
						result = BinsinessInterceptor.ACCESS_PERMIT;
					}
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return result;
	}
	
	public int toStatisticalWorks(ActionInvocation actionInvocation){
		int result = BinsinessInterceptor.ACCESS_DENIAL;
		try{
			Employee employee = ActionUtil.getSelectEmployee();
			if(employee != null && employee.getCompany() != null && employee.getType() == 2){
				result = BinsinessInterceptor.ACCESS_PERMIT;
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return result;
	}
	
	public int toStatisticsFinancial(ActionInvocation actionInvocation){
		int result = BinsinessInterceptor.ACCESS_DENIAL;
		try{
			Employee employee = ActionUtil.getSelectEmployee();
			if(employee != null && employee.getCompany() != null && employee.getType() == 2){
				result = BinsinessInterceptor.ACCESS_PERMIT;
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return result;
	}
	
	public int loadWorkDocContent(ActionInvocation actionInvocation){
		try{
			Employee employee = ActionUtil.getSelectEmployee();
			ActionContext actionContext = actionInvocation.getInvocationContext();
			HttpServletRequest request = (HttpServletRequest)actionContext.get(StrutsStatics.HTTP_REQUEST);
			String workDocId = request.getParameter("workDocId");
			String ajaxLoad = request.getParameter("ajaxLoad");
			if(employee != null && CommUtils.isValidStr(workDocId) && "1".equals(ajaxLoad)){
				WorkDoc workDoc = this.commonManager.findEntityByPK(WorkDoc.class, Long.parseLong(workDocId));
				if(workDoc != null){
					String shareUsers = workDoc.getShareUsers();
					if(CommUtils.isValidStr(shareUsers)){
						String readUserIds = shareUsers.substring(shareUsers.indexOf(";") + 1, shareUsers.length());
						if(CommUtils.isValidStr(readUserIds)){
							if(readUserIds.indexOf("," + employee.getId() + ",") == -1){
								workDoc.setShareUsers(workDoc.getShareUsers() + employee.getId() + ",");
							}
						}else{
							workDoc.setShareUsers(shareUsers.substring(0, shareUsers.indexOf(";") + 1) + "," + employee.getId() + ",");
						}
					}else{
						workDoc.setShareUsers(";," + employee.getId() + ",");
					}
					this.commonManager.modifyEntity(workDoc);
					
					//未读条数
					List<WorkDoc> allDocs = new ArrayList<WorkDoc>();
					Long parentId = workDoc.getParentId();
					if(parentId != null && parentId != 0){
						allDocs = this.commonManager.findByCustomizedSQL(WorkDoc.class, " parentId = " + parentId + " or id = " + parentId);
					}else{
						allDocs.add(workDoc);
					}
					Integer unReadCount = 0;
					for(WorkDoc doc: allDocs){
						shareUsers = doc.getShareUsers();
						if(CommUtils.isValidStr(shareUsers) && shareUsers.indexOf(";") != -1){
							String readedUser = shareUsers.substring(shareUsers.indexOf(";") + 1, shareUsers.length());
							if(!CommUtils.isValidStr(readedUser) || readedUser.indexOf("," + employee.getId() + ",") == -1){
								unReadCount++;
							}
						}else{
							unReadCount++;
						}
					}
					if(unReadCount == 0){
						unReadCount = null;
					}
					if(unReadCount == 1 && allDocs.size() == 1){ //NEW
						unReadCount = 0;
					}
					if(unReadCount > 5){
						unReadCount = 5;
					}
					request.setAttribute("unReadCount", unReadCount);
				}
			}
		}catch(RuntimeException e){
			e.printStackTrace();
		}
		return BinsinessInterceptor.ACCESS_PERMIT;
	}

	public CommonManager getCommonManager(){
		return commonManager;
	}

	public void setCommonManager(CommonManager commonManager){
		this.commonManager = commonManager;
	}

}
